import { runCommand } from ".";
const setCellType = (firCell, cuCell) => {
    const celltype = cuCell.cellType();
    firCell.cellType(celltype);
    firCell.backColor(cuCell.backColor());
    firCell.backgroundImage(cuCell.backgroundImage());
    firCell.backgroundImageLayout(cuCell.backgroundImageLayout());
    firCell.textIndent(cuCell.textIndent());
    firCell.borderBottom(cuCell.borderBottom());
    firCell.borderLeft(cuCell.borderLeft());
    firCell.font(cuCell.font());
    firCell.vAlign(cuCell.vAlign());
    firCell.hAlign(cuCell.hAlign());
    firCell.wordWrap(cuCell.wordWrap());
    firCell.textDecoration(cuCell.textDecoration());
    firCell.borderRight(cuCell.borderRight());
    firCell.borderTop(cuCell.borderTop());
    firCell.value(cuCell.value())
    firCell.formula(cuCell.formula());
    cuCell.cellType(undefined);
    cuCell.backgroundImage(undefined);
    cuCell.textIndent(undefined);
    cuCell.formula(undefined);
    cuCell.value(undefined)
}
export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        runCommand(context, options, isUndo, (sheet, options) => {
            const { row, rowCount = 1 } = options;
            sheet.addRows(row, rowCount);
            const tables = sheet.tables.all();
            for (let i = 0; i < tables.length; i++) {
                const table = tables[i];
                const { row: tableRow, rowCount: tRowCount, col: tableCol, colCount: tColCount } = table.dataRange();
                if (row >= tableRow && row <= tableRow + tRowCount) {

                    if (table.bindingPath()) {
                        
                        let typeRow;
                        // 明细表需要将cellType挪到第一行
                        for (let c = tableCol; c < tableCol + tColCount; c++) {
                            const firCell = sheet.getCell(tableRow, c);
                            if (!(firCell.cellType() instanceof window.FieldCellType)) {
                                if (typeRow) {
                                    const cuCell = sheet.getCell(typeRow, c);
                                    const celltype = cuCell.cellType();
                                    if (celltype instanceof window.FieldCellType) {
                                        setCellType(firCell, cuCell);
                                        continue;
                                    }
                                }
                                for (let r = tableRow; r < tableRow + tRowCount; r++) {
                                    const cuCell = sheet.getCell(r, c);
                                    const celltype = cuCell.cellType();
                                    if (celltype instanceof window.FieldCellType) {
                                        setCellType(firCell, cuCell);
                                        typeRow = r;
                                        continue;
                                    }

                                }
                            }
                        }

                    } else if (table.name().startsWith('custom')) {
                        for (let c = tableCol; c < tableCol + tColCount; c++) {
                            const orgCell = sheet.getCell(row + rowCount, c);
                            // const backColor = orgCell.backColor();
                            // //backgroundImageLayout: cell.backgroundImageLayout(),
                            // const borderBottom = orgCell.borderBottom();
                            // const borderLeft = orgCell.borderLeft();
                            // const borderRight = orgCell.borderRight();
                            // const borderTop = orgCell.borderTop();
                            for (let r = row; r < row + rowCount; r++) {
                                const nCell = sheet.getCell(r, c);
                                nCell.backColor(orgCell.backColor());
                                nCell.borderBottom(orgCell.borderBottom());
                                nCell.borderLeft(orgCell.borderLeft());
                                nCell.borderRight(orgCell.borderRight());
                                nCell.borderTop(orgCell.borderTop());
                            }
                        }
                    }
                }
            }

        })
    }
};
